﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JBeamLibrary;
using DriveBeamEditor.Controls.General;

namespace DriveBeamEditor.Forms
{
    public partial class PreferencesForm : Form
    {
        #region CTORs

        public PreferencesForm()
        {
            InitializeComponent();
            ReadConfigFile();
        }

        #endregion

        #region Overrides

        protected override void OnClosed(EventArgs e)
        {
            SaveConfigFile();

            base.OnClosed(e);
        }

        #endregion

        #region Events

        private void ctrlGamePath_ValueChanged(object sender, EventArgs e)
        {
            
        }

        private void kvcGamePath_EditValueChanged(object sender, EventArgs e)
        {
            btGamePath.InitialDirectory = kvcGamePath.Value;
        }

        private void btResetUserName_Click(object sender, EventArgs e)
        {
            Config.InitUserName();
            kvcUserName.Value = Config.Map[kvcUserName.Key];
        }

        #endregion

        #region Methods

        private void ReadConfigFile()
        {
            /* Read the config file */
            Config.Read();

            /* Assign values to controls */
            foreach (DictionaryEntry entry in Config.Map)
            {
                /* Game info controls */
                var gameCtrl = dgbGameInfo.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals((string)entry.Key));
                if (gameCtrl != null)
                {
                    gameCtrl.Value = (string)entry.Value;
                    continue;
                }

                /* App info controls */
                var appCtrl = dgbAppInfo.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals((string)entry.Key));
                if (appCtrl != null)
                {
                    appCtrl.Value = (string) entry.Value;
                    continue;
                }
            }
        }

        private void SaveConfigFile()
        {
            /* Update config */
            for (var i = 0; i < Config.Map.Count; i++)
            {
                /* Get current element */
                var entry = Config.Map[i];

                /* Game info controls */
                var gameCtrl = dgbGameInfo.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals(entry.Key));
                if (gameCtrl != null)
                {
                    Config.Map.Set(entry.Key, gameCtrl.Value);
                    continue;
                }

                /* App info controls */
                var appCtrl = dgbAppInfo.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals(entry.Key));
                if (appCtrl != null)
                {
                    Config.Map.Set(entry.Key, appCtrl.Value);
                    continue;
                }
            }

            /* Save config file */
            Config.Save();
        }

        #endregion
    }
}
